package com.parkinglot.service.Impl;

import com.parkinglot.core.Result;
import com.parkinglot.dao.MonthbilRecordDao;
import com.parkinglot.dao.UserDao;
import com.parkinglot.parmeter.*;
import com.parkinglot.pojo.MonthbilRecord;
import com.parkinglot.pojo.User;
import com.parkinglot.service.MonthbilRecordService;
import com.parkinglot.service.OrderService;
import com.parkinglot.util.CarNumberInit;
import com.parkinglot.util.DateUtils;
import com.parkinglot.util.RandomsUtil;
import com.parkinglot.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * (MonthbilRecord)表服务实现类
 *
 * @author makejava
 * @since 2020-11-13 15:29:06
 */
@Service("monthbilRecordService")
@Slf4j
public class MonthbilRecordServiceImpl implements MonthbilRecordService {
    @Resource
    private MonthbilRecordDao monthbilRecordDao;
    @Resource
    private UserDao userDao;
    @Resource
    private CarNumberInit carNumberInit;
    @Resource
    private OrderService orderService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MonthbilRecord queryById(Integer id) {
        return this.monthbilRecordDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<MonthbilRecord> queryAllByLimit(int offset, int limit) {
        return this.monthbilRecordDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param monthbilRecord 实例对象
     * @return 实例对象
     */
    @Override
    public MonthbilRecord insert(MonthbilRecord monthbilRecord) {
        this.monthbilRecordDao.insert(monthbilRecord);
        return monthbilRecord;
    }
    @Override
    public MonthbilRecord insertoverride(MonthRecordParm monthRecordParm) {
        int code=0;
        MonthbilRecord monthbilRecord=new MonthbilRecord();
        MonthbilRecord monthbilRecord1=null;
     try {
         monthRecordParm.setMonthOrder(RandomsUtil.buildRandom());
         BeanUtils.copyProperties(monthRecordParm,monthbilRecord);
         code= monthbilRecordDao.insert(monthbilRecord);
         if(code>0){
              monthbilRecord1=   monthbilRecordDao.queryAllone(monthRecordParm);
         }
     }catch (Exception e){
        e.printStackTrace();
     }
        return monthbilRecord1;
    }

    /**
     * 修改数据
     *
     * @param monthbilRecord 实例对象
     * @return 实例对象
     */
    @Override
    public MonthbilRecord update(MonthbilRecord monthbilRecord) {
        this.monthbilRecordDao.update(monthbilRecord);
        return this.queryById(monthbilRecord.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.monthbilRecordDao.deleteById(id) > 0;
    }

    @Override
    public List<UserMonthRecordVo> getmonthrecord(UserqueryParm userqueryParm) {
        List<UserMonthRecordVo>list=null;
        User user=new User();
        try {
            BeanUtils.copyProperties(userqueryParm,user);
            list=monthbilRecordDao.getmonthrecord(user);
            if(list.size()>0){
                return list;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    @Override
    public List<UserMonthRefunVo> getmonthtuikuan(UserqueryParm userqueryParm) {
        List<UserMonthRefunVo>list=null;
        User user=new User();
        try{
            BeanUtils.copyProperties(userqueryParm,user);
            list=monthbilRecordDao.getmonthtuikuan(user);
            if(list.size()>0){
                return list;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    @Override
    public MonthRecordVo countvip(MonthbilRecord monthbilRecord) {
        MonthRecordVo monthRecordVo= monthbilRecordDao.countvip(monthbilRecord);
        return monthRecordVo;
    }

    @Override
    public MonthRecordVo coungaragetvip(MonthRecordParm monthRecordParm) {
        MonthbilRecord monthbilRecord1=new MonthbilRecord();
        MonthRecordVo monthRecordVo=null;
        try{
            BeanUtils.copyProperties(monthRecordParm,monthbilRecord1);
            monthRecordVo=monthbilRecordDao.coungaragetvip(monthbilRecord1);
        }catch (Exception e){
            e.printStackTrace();
        }
        return monthRecordVo;
    }

    @Override
    public List<MonthbillUserRecordVo> coungarageusertvip(MohuqueryParm mohuqueryParm) {
        List<MonthbillUserRecordVo>list=null;
        try {
           list= monthbilRecordDao.coungarageusertvip(mohuqueryParm);
           if(list.size()>0){
               return list;
           }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    @Override
    public List<MonthbillUserRecordVo> userrechargerecord(String gardecode) {
        List<MonthbillUserRecordVo>list=null;
        try{
          list=  monthbilRecordDao.userrechargerecord(gardecode);
          if(list.size()>0)
              return list;
        }catch (Exception e){
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    @Override
    public List<MonthbillUserRecordVo> userrefundrecord(String gardecode) {
        List<MonthbillUserRecordVo>list=null;
        try{
            list=  monthbilRecordDao.userrefundrecord(gardecode);
            if(list.size()>0)
                return list;
        }catch (Exception e){
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    @Override
    public List<MonthbilRecord>  queryAll(MonthRecordParm monthRecordParm) {
        MonthbilRecord monthbilRecord=new MonthbilRecord();
        BeanUtils.copyProperties(monthRecordParm,monthbilRecord);
        List<MonthbilRecord> monthRecordVo=monthbilRecordDao.queryAll(monthbilRecord);
        return monthRecordVo;
    }

    @Override
    public MonthbilRecord queryAllone(MonthRecordParm monthRecordParm) {

        return monthbilRecordDao.queryAllone(monthRecordParm);
    }

    @Override
    public MonthbilRecord queryAllone(String monthorder) {
        return monthbilRecordDao.queryAllone(monthorder);
    }

    /**
     * 是否是会员
     * @param usercarMonthVipParm
     * @return
     */
    @Override
    public UsercarMonthVipVo queryexistencevip(UsercarMonthVipParm usercarMonthVipParm) {
        UsercarMonthVipVo usercarMonthVipVo=null;
        try {
            usercarMonthVipVo=monthbilRecordDao.queryexistencevip(usercarMonthVipParm);
        }catch (Exception e){
            e.printStackTrace();
        }
        return usercarMonthVipVo;
    }

    /**
     * 月票货期处理  30天
     * @param monthVipParm
     * @return
     */
    @Override
    public Result monthcipguoqi(MonthVipParm monthVipParm) {
        MonthbilRecord monthbilRecord=new MonthbilRecord();
        MonthbilRecordVo monthbilRecordVo=null;
        try{
            BeanUtils.copyProperties(monthVipParm,monthbilRecord);
            monthbilRecordVo=   monthbilRecordDao.monthcipguoqi(monthbilRecord);
            if(monthbilRecordVo != null){
             String endtime=   monthbilRecordVo.getMonthEndtime();
              String newendtime=  DateUtils.dateadd(endtime,1);
              if(newendtime.equals(DateUtils.getFormatDate(new Date()))){
                  User user=new User();
                  user.setVipstatus(0);
                  user.setUserid(monthbilRecordVo.getMonthUserid());
                  monthbilRecordVo.setStatus(0);
                userDao.update(user);
              }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.success(monthbilRecordVo);
    }

    @Override
    public Result getusercarisvip(String garageid, String userid,String carnumber) {
       List<CarMonthvipVo>list= monthbilRecordDao.getusercarisvip(garageid,userid);

        return Result.success(list);
    }


}